package udf.string;

import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;

import util.io.FileInput;
import util.io.ReadFileInJAR;

public class CleanTag {

	/**
	 * @param args
	 */
	/**
	 * @param args   
	 */

	private int THRESHOLD_LENGTH = 0;
	private int THRESHOLD_LENGTH_UPPER = 30;

	/**
	 * Returns True if the tag : contains less than 3 characters is
	 */

	private HashSet<String> stopWords = new HashSet<String>();

	public CleanTag(String path, boolean something) {
		
		FileInput in =new FileInput(path);
		
		String line = in.readString();
		
		while(line!=null){
		
		String t[]= line.trim().split("\t");
 //	System.out.println("adding " + t[0]);
		stopWords.add(t[0]);
		line=in.readString();
		}
	}

	public CleanTag(String path) throws IOException {

		ReadFileInJAR reader = new ReadFileInJAR();

		LinkedList<String> lines = reader.readFromJARFile(path);

		for (String line : lines) {
			String li[] = line.split("\t");
			stopWords.add(li[0].trim().toLowerCase());

		}

	}

	public boolean isTagTrash(String tag) {

		// does not contain only alpha and numerical characters

		tag = tag.trim().toLowerCase();
		// length less than 3
		
		
		if(tag.equals("")) return true;
		
		
		if (tag.length() <= THRESHOLD_LENGTH) {

			return true;
		}

		if (tag.length() >= THRESHOLD_LENGTH_UPPER) {

			return true;
		}
		// contains non-alphanumerical characters
		// , . - and _ are allowed for now

		if (tag.matches(".*[^a-zA-Z0-9-\\.,_].*")) {

			return true;
		}
		// contains .com, .org, www. ,etc.

		if (tag.matches("www|http|ftp|ssh|html|css|.com|.org|.nl|href")) {

			return true;
		}

		// the tag match a word in the stop word list
		if (stopWords.contains(tag)) {

			return true;
		}

		return false;

	}
	
	
	
	public boolean isTagTrashignoreStopWords(String tag) {

		// does not contain only alpha and numerical characters

		tag = tag.trim().toLowerCase();
		// length less than 3
		
		
		if(tag.equals("")) return true;
		
		
		if (tag.length() <= THRESHOLD_LENGTH) {

			return true;
		}

		if (tag.length() >= THRESHOLD_LENGTH_UPPER) {

			return true;
		}
		// contains non-alphanumerical characters
		// , . - and _ are allowed for now

		if (tag.matches(".*[^a-zA-Z0-9-\\.,_].*")) {

			return true;
		}
		// contains .com, .org, www. ,etc.

		if (tag.matches("www|http|ftp|ssh|html|css|.com|.org|.nl|href")) {

			return true;
		}

		// the tag match a word in the stop word list
		

		return false;

	}
	public boolean isStopWord(String word){
		if(stopWords.contains(word)) return true;
		return false;
		
		
	}

	public static String normalizeSeparators(String a) {

		a = a.replaceAll("[:_;.,\\\\/-]+", "_");

		if (a.endsWith("_")) {
			a = a.substring(0, a.length() - 1);

		}

		if (a.startsWith("_")) {
			a = a.substring(1, a.length());

		}

		return a;

	}

	public static String removeInvalidCharacters(String a) {

		a = a.replaceAll("[\"\\[\\]{}()~`!+@#$%^&*=|'?<>]+", "");

		return a;

	}

	public static String normalizeTag(String a) {

		a = normalizeSeparators(a);
		a = removeInvalidCharacters(a);
		a = normalizeSeparators(a);

		return a;
	}

	public static void main(String[] args) {

		String a = "asdfas_____---___asdfas_";
		a = a.replaceAll("[:_;.,\\\\/-]+", "_");
		System.out.println(normalizeSeparators(a));
		// CleanTag cleaner = new CleanTag();
		// System.out.println(cleaner.isTagTrash(""));
	}

}
